package com.ehotting.edsta.mqbd.monitor.task.util;

import org.apache.commons.net.telnet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class TelnetChecker {

    private final static Logger logger = LoggerFactory.getLogger(TelnetChecker.class);

    public static boolean checkService(String host,int port){
        boolean isService = false;
        TelnetClient telnetClient = new TelnetClient();
        try{
            telnetClient.setDefaultTimeout(5000);
            telnetClient.connect(host, port);
            if (telnetClient.isConnected()) {
                logger.info("Service[ip:{},port:{}] is available.",host,port);
                isService = true;
            } else {
                logger.error("Service[ip:{},port:{}] is  unavailable.",host,port);
            }
        } catch (Exception e) {
            logger.error("Service[ip:{},port:{}] error occurred while checking service availability:{}" ,host,port,e.getMessage());
        }finally {
            try {
                telnetClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isService;
    }


}